#include "ihm.h"
#include "qmainsettings.h"

IHM::IHM(QWidget *parent, Qt::WFlags flags)
   : QWidget(parent, flags)
{
	ui.setupUi(this);
	init();
	m_open.setFilter("*.exp");
	connect(ui.open,SIGNAL(clicked()),&m_open,SLOT(show()));
	connect(&m_open,SIGNAL(fileSelected(const QString&)),this,SLOT(analyse(const QString&)));
	connect(ui.errors,SIGNAL(itemSelectionChanged()),this,SLOT(selectionListChanged()));
	connect(ui.acceptterms,SIGNAL(clicked()),this,SLOT(validTerms()));
	connect(ui.searchAll,SIGNAL(clicked()),this,SLOT(searchAll()));
	connect(ui.settings,SIGNAL(clicked()),this,SLOT(showSettings()));
	connect(ui.report,SIGNAL(clicked()),this,SLOT(showReport()));
	connect(ui.bom,SIGNAL(clicked()),this,SLOT(generateBOM()));	
}

IHM::~IHM()
{
	if(m_reportDisplay)
	{
		delete m_reportDisplay;
		m_reportDisplay = NULL;
	}
}


void IHM::init()
{
	m_report = new ReportFile(".");
	m_terms = new QDisplayTerms(ui.searchterms);
	m_buttons = new QButtonManager(ui.errors,ui.errorButton);
	m_interface = new Interface(__getBaseName(),m_report);
	
	m_plp = new QDisplayCodePLP(ui.codeplp_layout);
	m_results = new QDisplayResult(ui.searchresults);
	m_display = new QDisplayField(ui.part);
  

  
	connect(m_display,SIGNAL(partModified(const QStringList&)),this,SLOT(changeCurrentLines(const QStringList&)));
	connect(m_results,SIGNAL(partModified(const QStringList&)),this,SLOT(changeCurrentLines(const QStringList& )));
	connect(m_plp,SIGNAL(addToFile(const QStringList&)),this,SLOT(addNewPLP(const QStringList&)));
	connect(m_plp,SIGNAL(modifyPart(const QStringList&)),this,SLOT(modifyPLP(const QStringList&)));
	m_settings = NULL;
	m_reportDisplay = NULL;
}

void IHM::analyse(const QString& exp)
{
   m_interface->analyseFile(exp);
   m_buttons->refresh(m_interface->getErrors());
}


void IHM::selectionListChanged()
{
	QListLine* line = (QListLine*) ui.errors->currentItem();
	if(line && line->isSelected())
	{
		ui.partBox->setTitle(line->text());
		QEXPPart* part = line->part();
		if(part)
		{
			m_display->setPartToDisplay(part);
			m_terms->setPartToDisplay(part);
			m_plp->setPartToDisplay(part);
			searchEquivalence(part);
		}
	}
}


void IHM::searchEquivalence(QEXPPart* part)
{
	QList<QStringList> search;

	for(int i=0;i<ui.searchterms->count();i++)
	{
		QSearchTerms* term = (QSearchTerms*) ui.searchterms->itemAt(i)->widget();
		if(term)
		{
			if(!term->terms().isEmpty())
				search << term->terms();
		}
	}

	m_results->setResultsToDisplay(m_interface->search(part->get(EXP_DESIGNATOR), search, 10));
}


void IHM::validTerms()
{
	QListLine* line = (QListLine*) ui.errors->currentItem();
	if(line)
	{
		QEXPPart* part = line->part();
		if(part)
		  searchEquivalence(part);
	}
}



void IHM::searchAll()
{
	QList<QStringList> terms;
	terms << ui.termsAll->text().split(" ");
	m_results->setResultsToDisplay(m_interface->search(EXP_ALL_DESIGNATOR,terms,0));
}



void IHM::showSettings()
{
	if(m_settings == NULL)
	{
		m_settings = new QSettingsIHM();
		connect(m_settings,SIGNAL(quitSettings()),this,SLOT(destroySettings()));
	}
	m_settings->show();
}


void IHM::showReport()
{
	if(m_reportDisplay == NULL)
		m_reportDisplay = new QDisplayReport(m_report);
	m_reportDisplay->display();
}

void IHM::destroySettings()
{
	if(m_settings)
	{
		delete m_settings;
		m_settings = NULL;
	}
}

void IHM::changeCurrentLines(const QStringList& newPart)
{
	QStringList fields;
	fields << EXP_CODE_PLP << EXP_VALUE << EXP_MANUFACTURER << EXP_REF_MANUFACTURER  << EXP_FOOTPRINT;
	m_interface->replace(m_buttons->actualLines(),fields,newPart);
	checkAgain(m_buttons->currrentId());
}


void IHM::addNewPLP(const QStringList& item)
{
	m_interface->addPlp(item);
}

void IHM::modifyPLP(const QStringList& part)
{
	QStringList goodField;
	goodField << part.first();
	goodField << part[5];
	goodField << part[10];
	goodField << part[11];
	goodField << QString();
	changeCurrentLines(goodField);
	checkAgain(m_buttons->currrentId());
}

void IHM::checkAgain(QString id)
{
	m_interface->checkCurrent();
	m_buttons->refresh(m_interface->getErrors());
	m_buttons->buttonChanged(id);
}

void IHM::generateBOM()
{
	m_interface->generateBOM();
}